package com.alipay.app.groupmeal.standard.base;

import android.accounts.NetworkErrorException;
import android.text.TextUtils;

import com.alipay.app.groupmeal.standard.constant.BaseConstant;
import com.alipay.app.groupmeal.standard.http.bean.resp.Result;
import com.blankj.utilcode.util.NetworkUtils;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.ResourceObserver;

public abstract class BaseResObserver<T> extends ResourceObserver<Result<T>> {

    private AbstractView mView;

    public BaseResObserver(AbstractView view) {
        this.mView = view;
    }

    @Override
    protected void onStart() {
        super.onStart();
        //请求前置判断
        if (!NetworkUtils.isConnected()) {
            onError(new NetworkErrorException("无网络，请检查你的网络设置"));
            return;
        }
    }

    @Override
    public void onNext(@NonNull Result<T> response) {
        if (response.isSuccess())
            doNext(response.getResult());
        else {
            //失败回调 内部做一些异常处理
            doError(String.valueOf(response.getCode()), response.getMessage());
        }
    }

    @Override
    public void onError(@NonNull Throwable e) {
        doError("-1", e.getMessage());
    }

    @Override
    public void onComplete() {
    }

    public abstract void doNext(T t);

    public abstract void doError(String code, String msg);

}
